数据分析

您所在的位置:网站首页 r语言 summarize 数据分析

数据分析

2023-01-01 02:35| 来源: 网络整理| 查看: 265

备忘录地址(用来查询一些参数)

https://www.maths.usyd.edu.au/u/UG/SM/STAT3022/r/current/Misc/data-visualization-2.1.pdf

R语言软件参数 R3.6.0 安装包、导入包 install.package('ggplot') library(ggplot) 首先加载数据集

这里用的是示例数据,R自带的一个包提供的数据集。 dslabs包是R中的一个数据科学工具包,它提供了一系列数据集和函数,用于帮助研究人员探索新的数据科学概念,进行数据分析和机器学习模型构建。

library(dslabs) data(murders) 定义ggplot对象 ggplot初始化图形

管道符(|>, %>%, %>%, %$% 和%%),|>是R4.1版本开始的自带的管道符,将左侧的输出结果传递给右侧命令,因为这里用的是3.6版本的所以就用%>%管道符,测试发现|>不能被识别。

ggplot(data = murders)##或者murders |> ggplot()

也可以把ggplot图赋值给一个对象p

p % ggplot() + LAYER 1 + LAYER 2 + … + LAYER N murders %>% ggplot()+ geom_point(aes(x = population/10^6, y = total))#添加散点图,添加x轴population,y轴total

运行结果

几何函数名称遵循以下模式:geom_X其中 X 是几何的名称,这里散点图是point,要用其他图的话可以去备忘录上的geoms上看。

方式二,在定义的对象p上叠加图层 p + geom_point(aes(x = population/10^6, y = total))#p是前面定义过的含有数据的初始图 继续添加图层

在散点图基础上添加一个文本组件

p + geom_point(aes(population/10^6, total)) + geom_text(aes(population/10^6, total, label = abb))#Label = abb 指的是标签 (label) 的缩写

运行结果

对文本组件进行参数调整 #size 控制点的大小, nudge_x 文本稍微向右或向左移动 p + geom_point(aes(population/10^6, total), size = 3) + geom_text(aes(population/10^6, total, label = abb), nudge_x = 1.5)

可以看到文本向右移动了 ,点也变大了 结果

全局与局部映射aes()

先查看下ggplot参数

args(ggplot)

在这里插入图片描述

全局映射:在定义对象时将坐标映射定义 p % ggplot(aes(population/10^6, total, label = abb))

然后就可以直接使用了,不用在每个组件中定义x轴,y轴

p + geom_point(size = 3) + geom_text(nudge_x = 1.5)

得到 结果

在全局映射中进行局部映射

想要单独让某个组件有自己的坐标

p + geom_point(size = 3) + geom_text(aes(x = 10, y = 800, label = "Hello there!"))

在这里插入图片描述 可以看到在(x=10,y=800)的坐标位置有个文本

控制坐标轴刻度

scale_x_continuous函数可以用来在指定的坐标轴上对数据进行缩放。

p + geom_point(size = 3) + geom_text(nudge_x = 0.05) + scale_x_continuous(trans = "log10") + scale_y_continuous(trans = "log10") #用于将坐标轴的连续数据变换为以10为底的对数尺度

在这里插入图片描述

标签和标题

使用xlab,ylab和ggtitle

p + geom_point(size = 3) + geom_text(nudge_x = 0.05) + scale_x_log10() + scale_y_log10() + xlab("Populations in millions (log scale)") + ylab("Total number of murders (log scale)") + ggtitle("US Gun Murders in 2010")

结果

给类别添加颜色 p % ggplot(aes(population/10^6, total, label = abb)) + geom_text(nudge_x = 0.05) + scale_x_log10() + scale_y_log10() + xlab("Populations in millions (log scale)") + ylab("Total number of murders (log scale)") + ggtitle("US Gun Murders in 2010") p + geom_point(size = 3, color ="green")#在散点图里添加color

在这里插入图片描述 可以选择按什么分类颜色,比如这个数据里有的region,使用aes

p + geom_point(aes(col=region))

在这里插入图片描述 上面的图例可以通过show.legend = FALSE删除

添加一条直线

这里代表全国的平均谋杀率 y=rx geom_abline是画线的,log10是因为之前坐标轴是转换到log10标准的 geom_abline(intercept, slope, linetype, color, size) 参数说明:intercept:y轴的截距 slope:斜率linetype:线条样式color:线条颜色size:线条宽度 pull()函数是R语言中用于提取对象的函数。它的作用是从已有的数据框、向量或列表中提取特定行或列的数据。

#计算斜率 r % summarize(rate = sum(total) / sum(population) * 10^6) %>% pull(rate) p % pull(rate) murders %>% ggplot(aes(population/10^6, total, label = abb)) + geom_abline(intercept = log10(r), lty = 2, color = "darkgrey") + geom_point(aes(col=region), size = 3) + geom_text_repel() + scale_x_log10() + scale_y_log10() + xlab("Populations in millions (log scale)") + ylab("Total number of murders (log scale)") + ggtitle("US Gun Murders in 2010") + scale_color_discrete(name = "Region") + theme_economist() 描述分布 条形图 murders %>% ggplot(aes(region)) + geom_bar()

在这里插入图片描述 不希望geom_bar计数,而是只绘制一个由比例变量提供高度的条形图。为此,提供x(类别)和y(值),并使用stat=“identity”选项。 先计算region每一类的数量然后传入下一行代码中

tab % count(region) %>% mutate(proportion = n/sum(n)) tab %>% ggplot(aes(region, proportion)) + geom_bar(stat = "identity")

在这里插入图片描述

直方图

用的是范例数据里的heights的数据,geom_histogram()函数,添加了标题,x轴标签

library(dslabs) data(heights) x % filter(sex == "Female") %>% ggplot(aes(height)) + geom_histogram(binwidth = 1, fill = "blue", col = "black") + xlab("Female heights in inches") + ggtitle("Histogram")

在这里插入图片描述

密度图

adjust参数控制密度图的平滑度,取值范围为0~1,默认值为1,值越大,则平滑度越高。

heights %>% filter(sex == "Female") %>% ggplot(aes(height)) + geom_density(fill="blue",adjust = 2)

在这里插入图片描述

箱线图 heights %>% ggplot(aes(sex,height)) + geom_boxplot()

在这里插入图片描述

使用qplot快速绘图

快速绘制一个向量值的直方图、两个向量值的散点图或使用分类向量和数值向量的箱线图 qplot(x, y, data, geom, color, fill, size, shape, alpha, etc.) 其中,x和y参数用于指定绘图变量,data参数指定数据集,geom参数用于指定绘图类型,color、fill、size、shape、alpha等参数则可用于控制图表的外观。

两个向量的值绘制散点图 data(murders) x % qplot(sex, height,data=.)

在这里插入图片描述 ##箱线图

heights %>% qplot(sex, height, data = ., geom = "boxplot")

在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3